package com.esender.strategy.filter;

import com.esender.common.constant.CacheConstant;
import com.esender.common.model.StandardSubmit;
import com.esender.strategy.client.BeaconCacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component

public class StrategyFilterContext {
    @Autowired
    private Map<String,StrategyFilter> strategyFilterMap;
    @Autowired
    private BeaconCacheClient beaconCacheClient;
    private static final String CLIENT_FILTERS = "clientFilters";

    /**
     * 当前startStrategyFilter方法用于管理校验链的顺序
     */
    public void startStrategyFilter(StandardSubmit standardSubmit){
        // 基于apiKey从缓存中获取要校验的策略
        String clientFilters = beaconCacheClient.hget(CacheConstant.CLIENT_BUSINESS + standardSubmit.getApiKey(), CLIENT_FILTERS);
        String[] clientFilterArray = clientFilters.split(",");
        if (null != clientFilterArray && clientFilterArray.length > 0){
            for (String clientFilter : clientFilterArray) {
                StrategyFilter strategyFilter = strategyFilterMap.get(clientFilter);
                if (null != strategyFilter){
                    strategyFilter.strategy(standardSubmit);
                }
            }
        }
    }
}
